import axios from 'axios' // 引入axios

export const $getJson = function(url) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url: process.env.VUE_APP_BASE_API + url,
      dataType: 'json',
      cache: false
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}

// attr：根据该属性排序；rev：升序1或降序-1，不填则默认为1
export const sortBy = function sortBy(attr, rev) {
  if (rev === undefined) { rev = 1 } else { (rev) ? 1 : -1 }
  return function(a, b) {
    a = a[attr]
    b = b[attr]
    if (a < b) { return rev * -1 }
    if (a > b) { return rev * 1 }
    return 0
  }
}
